﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Billing.Utilities;
using System.Data;
using Billing.Controls;

namespace Billing.Windows
{
    public partial class AddAdvance : Window
    {
        public bool IsExit { get; set; }
        List<AdvanceOrBillItemControl> mainList = new List<AdvanceOrBillItemControl>();
        public AddAdvance()
        {
            InitializeComponent();

            try
            {
                lstBranch.ItemsSource = Database.GetBranches();
                lstBranch.DisplayMemberPath = "BranchName";
                lstBranch.SelectedValuePath = "BranchID";
                lstBranch.SelectedIndex = 0;
                lstBranch.Focus();
                TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Previous);
                UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                if (elementWithFocus != null)
                    elementWithFocus.MoveFocus(request);
                //lstEmpName.ItemsSource = Database.GetEmployees();
                //lstEmpName.DisplayMemberPath = "EmpName";
                //lstEmpName.Focus();
                //lstEmpName.SelectedIndex = 0;
                mainList.Add(new AdvanceOrBillItemControl());
                foreach (DataRow row in Database.GetDebitTransactionsAddedToday(1).Rows)
                {
                    AdvanceOrBillItemControl item = new AdvanceOrBillItemControl();
                    item.lblEmp.Content = row[0].ToString();
                    item.lblAmount.Content = row[1].ToString();
                    item.lblDate.Content = DateTime.Parse(row[2].ToString()).ToString("dd/MMM/yyyy");
                    mainList.Add(item);
                }

                lbAdvanceGrid.ItemsSource = mainList;
            }
            catch (Exception) { }
        }

        private void date_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    if (date.IsFocusedOnDate)
                        date.SetFocusOnMonth();
                    else if (date.IsFocusedOnMonth)
                        date.SetFocusOnYear();
                    else if (date.IsFocusedOnYear)
                        date.SetFocusOnHours();
                }
                else if (e.Key == Key.Right && date.IsFocusedOnDate)
                    date.SetFocusOnMonth();
                else if (e.Key == Key.Left && date.IsFocusedOnDate)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Previous);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);
                    //lstEmpName.Focus();
                }
            }
            catch (Exception) { }
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Escape)
                {
                    this.IsExit = true;
                    this.Close();
                }
                else if (e.Key == Key.F1)
                {
                    if (MessageBoxResult.OK == MessageBox.Show("Do you want to Add Advance?", "Confirm Add Advance", MessageBoxButton.OKCancel))
                    {
                        double value = 0;
                        if (!(double.TryParse(txtAmount.Text, out value)))
                        {
                            MessageBox.Show("Invalid Amount. Enter Valid Amount", "Invalid Amount", MessageBoxButton.OK, MessageBoxImage.Error);
                            txtAmount.Clear();
                            txtAmount.Focus();
                            TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Previous);
                            UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                            if (elementWithFocus != null)
                                elementWithFocus.MoveFocus(request);
                            return;
                        }

                        int name = ((Billing.Utilities.Employee)(lstEmpName.SelectedItem)).EmpID;
                        DateTime selectedDate = new DateTime(date.nudYear.Value, date.nudMonth.Value, date.nudDate.Value);

                        int result = Database.AddAdvanceOrBill(name.ToString(), txtAmount.Text, AdvanceOrBill.Advance, selectedDate.ToString("MM/dd/yyyy"));
                        if (result < 0)
                        {
                            MessageBox.Show("Some unknown error has occurred.", "Unknown Error.", MessageBoxButton.OK);
                            return;
                        }
                        else
                        {
                            MessageBox.Show("Advance Added Successfully.                        ", "Success", MessageBoxButton.OK);
                            //txtAmount.Clear();
                            //mainList.Clear();
                            //lbAdvanceGrid.ItemsSource = null;
                            //mainList.Add(new AdvanceOrBillItemControl());
                            //foreach (DataRow row in Database.GetDebitTransactionsAddedToday(1).Rows)
                            //{
                            //    AdvanceOrBillItemControl item = new AdvanceOrBillItemControl();
                            //    item.lblEmp.Content = row[0].ToString();
                            //    item.lblAmount.Content = row[1].ToString();
                            //    item.lblDate.Content = DateTime.Parse(row[2].ToString()).ToString("dd/MMM/yyyy");
                            //    mainList.Add(item);
                            //}

                            //lbAdvanceGrid.ItemsSource = mainList;
                            //lstBranch.Focus();
                            //e.Handled = true;
                            this.Close();
                        }
                    }
                }
            }
            catch (Exception) { }
        }

        private void lstEmpName_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                department.Content = (((System.Windows.Controls.ListBox)(sender)).SelectedItems[0] as Employee).Department;
                designation.Content = (((System.Windows.Controls.ListBox)(sender)).SelectedItems[0] as Employee).Designation;
                //branch.Content = (((System.Windows.Controls.ListBox)(sender)).SelectedItems[0] as Employee).Branch;
            }
            catch (Exception) { }
        }

        private void lstEmpName_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                    txtAmount.Focus();
                else if (e.Key == Key.Left)
                    lstBranch.Focus();
                else if (e.Key == Key.Right)
                    txtAmount.Focus();
            }
            catch (Exception) { }
        }

        private void lbAdvanceGrid_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                    lstBranch.Focus();
            }
            catch (Exception) { }
        }

        private void txtAmount_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                    date.SetFocusOnDate();
                else if (e.Key == Key.Left)
                    lstEmpName.Focus();
                else if (e.Key == Key.Right)
                    date.SetFocusOnDate();
            }
            catch (Exception) { }
        }

        private void lstBranch_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            lstEmpName.ItemsSource = Database.GetEmployeesByBranch(lstBranch.SelectedValue.ToString());
            lstEmpName.DisplayMemberPath = "EmpIDName";
            lstEmpName.SelectedIndex = 0;
        }

        private void lstBranch_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                //if (e.Key == Key.Enter)
                //    lstEmpName.Focus();
                //else 
                    if (e.Key == Key.Right)
                    lstEmpName.Focus();
            }
            catch (Exception) { }
        }
    }
}
